
#include "BufferString.h"

BufferString::BufferString()
        : ptr(NULL), length(0) {}

BufferString::BufferString(const char *str)
        : ptr(str) {
    length = (int) strlen(str);
}

bool BufferString::isEmpty() {
    return ptr == NULL || length <= 0;
}

int BufferString::toInt() {
	for (int i=0; i<length; i++) {
		if (ptr[i] > '9' || ptr[i] < '0') {
			return -1;
		}
	}

	int num = 0;
	for (int i=length-1; i>=0; i--) {
		num *= 10;
		num += ptr[i] - '0';
	}
	return num;
}

BufferString& BufferString::operator=(BufferString &str) {
    ptr = str.ptr;
    length = str.length;
    return *this;
}

BufferString &BufferString::operator=(const char *str) {
    ptr = str;
    length = (int) strlen(str);
    return *this;
}

bool BufferString::operator==(BufferString &str) {
    if (length != str.length) {
        return false;
    }
    if (ptr == str.ptr) {
        return true;
    }
    return !strncmp(ptr, str.ptr, (size_t) length);
}

bool BufferString::operator==(const char *str) {
    if (length != (int) strlen(str)) {
        return false;
    }
    return !strncmp(ptr, str, (size_t) length);
}
